import { ref } from 'vue'
import { defineStore } from 'pinia'
export interface IUserInfo {
    avatar: string
    nickname: string
    organizationId: number
    type: number
    userId: string
    username: string
}
export const useUserinfo = defineStore('userInfo', () => {
    const cacheLoginInfo = localStorage.getItem('login')
    let userInfo
    let userScope
    let roleCodes
    if (cacheLoginInfo) {
        const {info, scope,attributes} = JSON.parse(cacheLoginInfo)
        userInfo = info
        userScope = scope
        roleCodes = attributes.roleCodes
    }
    const user = ref(userInfo)
    const scope = ref<string[]>(userScope)
    const roles = ref<string[]>(roleCodes)
    const setUser = ({ info,scopes,attributes }: { info: IUserInfo ,scopes: string[],attributes:{roleCodes:string[]}}) => {
        user.value = info
        scope.value = scopes
        roles.value=attributes.roleCodes
    }
    return { user, setUser ,scope,roles}
})